//
//  LLImagePicker.m
//  TaoTuo
//
//  Created by admin on 2019/11/7.
//  Copyright © 2019 admin. All rights reserved.
//

#import "LLImagePicker.h"

@implementation LLImagePicker

+ (void)imagePickerWithMaxImagesCount:(NSInteger)maxImagesCount delegate:(id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>)delegate baseViewController:(UIViewController *)baseViewController didFinishPickingPhotosHandle:(void (^)(NSArray<UIImage *> * _Nonnull, BOOL))didFinishPickingPhotosHandle canEdit:(BOOL)canEdit
{
    if (@available(iOS 13.0, *)) {
        [self gotoSystemPhotoLibraryChooseImageAndDelegate:delegate canEdit:canEdit];
        return;
    }
    
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:maxImagesCount delegate:nil];
    imagePickerVc.allowPickingOriginalPhoto = NO;   // 原图按钮隐藏
    imagePickerVc.allowPickingGif = NO;     // 不能选中gif图片
    imagePickerVc.allowPickingVideo = NO;   // 不能选择视频
    imagePickerVc.sortAscendingByModificationDate = NO;    //照片排列按修改时间升序
//    imagePickerVc.allowCrop = YES;
    imagePickerVc.showSelectedIndex = YES;
    imagePickerVc.allowCameraLocation = NO;
    imagePickerVc.allowCrop = canEdit;

    // 得到用户选择的照片.
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        if (didFinishPickingPhotosHandle) {
            didFinishPickingPhotosHandle(photos, isSelectOriginalPhoto);
        }
    }];

    imagePickerVc.modalPresentationStyle = UIModalPresentationFullScreen;
    [baseViewController presentViewController:imagePickerVc animated:YES completion:nil];
}

+ (void)imagePickerWithMaxImagesCount:(NSInteger)maxImagesCount delegate:(id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>)delegate baseViewController:(UIViewController *)baseViewController didFinishPickingPhotosHandle:(nonnull void (^)(NSArray<UIImage *> * _Nonnull, BOOL))didFinishPickingPhotosHandle
{
    
    [self imagePickerWithMaxImagesCount:maxImagesCount delegate:delegate baseViewController:baseViewController didFinishPickingPhotosHandle:didFinishPickingPhotosHandle canEdit:YES];
    
}

///调用 系统相册 或 拍照选取图片
+ (void)gotoSystemPhotoLibraryChooseImageAndDelegate:(id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>)delegate canEdit:(BOOL)canEdit
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *pictureAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self choosePictureFromPhotoLibraryWithDelegate:delegate canEdit:canEdit];
    }];
    
    UIAlertAction *takePhotoAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self takePhotoWithDelegate:delegate];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {}];
    
    [alertVC addAction:pictureAction];
    [alertVC addAction:takePhotoAction];
    [alertVC addAction:cancelAction];
    
    ///为了适配ipad，直接present的话，在ipad上会闪退
    UIPopoverPresentationController *popover = alertVC.popoverPresentationController;
    if (popover) {
        popover.sourceView = [UIApplication sharedApplication].keyWindow;
        popover.sourceRect = CGRectMake(0, 0, SCREEN_WIDTH, StatusBarAndNavBar_Height);
        popover.permittedArrowDirections = UIPopoverArrowDirectionLeft;
    }
    
    [[LLRouteTool getCurrentNavigationController] presentViewController:alertVC animated:YES completion:nil];
}


// 去相册选取图片
+ (void)choosePictureFromPhotoLibraryWithDelegate:(id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>)delegate canEdit:(BOOL)canEdit
{
    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
    pickerController.delegate = delegate;
    pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pickerController.allowsEditing = canEdit;
    [[LLRouteTool getCurrentNavigationController] presentViewController:pickerController animated:YES completion:nil];
}

// 拍照
+ (void)takePhotoWithDelegate:(id<UINavigationControllerDelegate,UIImagePickerControllerDelegate>)delegate
{
    // 摄像机是否可用
    BOOL cameraCanUse = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
    if (cameraCanUse) {
        //是否有权限访问相机
        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied) {
            [LLAlertUtilies showSystemSetAlertViewWithTitle:@"开启相机权限" message:@"开启后才能拍照" cancelTitle:nil confirmTitle:@"去开启"];
            return;
        }
        
        UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
        pickerController.delegate = delegate;
        pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        pickerController.allowsEditing = YES;
        [[LLRouteTool getCurrentNavigationController] presentViewController:pickerController animated:YES completion:nil];
        
    } else {
        [MBProgressHUD showAutoHideMessage:@"相机不可用"];
    }
    
}


@end
